#include <stdio.h>
#include "stdafx.h"
#include "CScene.h"

CScene::CScene(void)
{
}

CScene::~CScene(void)
{
}

void CScene::setcamara(float x, float y, float z, float roll, float pitch, float yaw)
{
	//set projection matrix
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);

	//set modelview matrix
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();
	glRotatef(-roll, 0.0, 0.0, 1.0);	
	glRotatef(-yaw, 0.0, 1.0, 0.0);
	glRotatef(-pitch, 1.0, 0.0, 0.0);	
	glTranslatef(-x, -y, -z);
}

void CScene::cambiarCamara(int n)
{
	m_Camara.g_x = m_pCamaras[n]->g_x;
	m_Camara.g_y = m_pCamaras[n]->g_y;
	m_Camara.g_z = m_pCamaras[n]->g_z;
	m_Camara.g_roll = m_pCamaras[n]->g_roll;
	m_Camara.g_pitch = m_pCamaras[n]->g_pitch;
	m_Camara.g_yaw = m_pCamaras[n]->g_yaw;	
}

void CScene::dibujaCamara(void)
{
	setcamara(m_Camara.g_x, m_Camara.g_y, m_Camara.g_z, m_Camara.g_roll, m_Camara.g_pitch, m_Camara.g_yaw);
}

void CScene::setLuz(int n)
{
		m_pLuces[n]->setLuces(n);
}

void CScene::loadFile(char *pFilename)
{
	FILE *pfile;  
	m_numLuces = 0;
	m_numCamaras = 0;	
	m_numObjects = 0;
	fopen_s(&pfile,pFilename, "r");
	if(pfile != NULL)
    {	
		float m_x, m_y, m_z, m_a1;
		float m_r, m_p, m_ya, m_a2;
		float m_px, m_py, m_pz, m_a3;
		float m_lx, m_ly ,m_lz, m_a4;
		char s[30];
		char line [256];
		
		fgets(line, 256, pfile);
		if(line[0]=='#' && line[1]=='o' && line[2]=='b')
		{		
				fgets(line, 256, pfile);
				while(line[0]!='#')
				{					
					sscanf(line, "%s, x=%f, y=%f, z=%f", &s, &m_x, &m_y, &m_z);
					m_pObjects[m_numObjects] = new CObj;
					m_pObjects[m_numObjects]->crearObjeto(s, m_x, m_y, m_z);
					m_numObjects++;
					fgets(line, 256, pfile);
				}
		}
		if(line[0]=='#' && line[1]=='l' && line[2]=='u')
		{		
				fgets(line, 256, pfile);
				while(line[0]!='#')
				{					
					sscanf(line, "a %f, %f, %f, %f / d %f, %f, %f, %f / p %f, %f, %f, %f / lma %f, %f, %f, %f",
						&m_x, &m_y, &m_z, &m_a1, &m_r, &m_p, &m_ya, &m_a2, &m_px, &m_py, &m_pz, &m_a3, &m_lx, 
						&m_ly, &m_lz, &m_a4);
					m_pLuces[m_numLuces] = new CLuces;
					m_pLuces[m_numLuces]->ambiente(m_x, m_y, m_z, m_a1);
					m_pLuces[m_numLuces]->difusa(m_r, m_p, m_ya, m_a2);
					m_pLuces[m_numLuces]->posicional(m_px, m_py, m_pz, m_a3);
					m_pLuces[m_numLuces]->luz_mod_amb(m_lx, m_ly, m_lz, m_a4);
					m_pLuces[m_numLuces]->setLuces(m_numLuces);
					m_numLuces++;
					fgets(line, 256, pfile);
				}
		}
		if(line[0]=='#' && line[1]=='c' && line[2]=='a')
		{				
				while(!feof(pfile))
				{					
					fgets(line, 256, pfile);
					sscanf(line, "c %f, %f, %f / a %f, %f, %f", &m_x, &m_y, &m_z, &m_r, &m_p, &m_ya);
					m_pCamaras[m_numCamaras] = new CCamaras;
					m_pCamaras[m_numCamaras]->coordenadas(m_x, m_y, m_z);
					m_pCamaras[m_numCamaras]->angulos(m_r, m_p, m_ya);					
					m_numCamaras++;					
		//			printf("\nc %f, %f, %f / a %f, %f, %f", m_x, m_y, m_z, m_r, m_p, m_ya);
				}				
		} 
		cambiarCamara(0);
	}
	else
		printf("No se puede abrir el archivo\n");
}